﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MathUtils.Extensions;

namespace GameName1
{
    class Camera
    {
        private float _speed;

        public Vector2 Position;
        public int ScreenWidth;
        public int ScreenHeight;
        public int CameraMovementRangeX;
        public int CameraMovementRangeY;

        public float Speed
        {
            get { return _speed; }
            set { _speed = Math.Max(1.0f, value); }
        }

        public Camera(){ }

        public Camera(float cameraSpeed)
        {
            Speed = cameraSpeed;
        }

        public void SetScreenAndCameraLimits(int screenX, int screenY, int cameraX, int cameraY)
        {
            ScreenWidth = screenX;
            ScreenHeight = screenY;
            CameraMovementRangeX = cameraX;
            CameraMovementRangeY = cameraY;
        }

        public void UpdateCamera(KeyboardState keyboardState, Vector2 playerPosition)
        {
            Vector2 motion = Vector2.Zero;
            if (keyboardState.IsKeyDown(Keys.NumPad4))
                motion.X--;
            if (keyboardState.IsKeyDown(Keys.NumPad6))
                motion.X++;
            if (keyboardState.IsKeyDown(Keys.NumPad8))
                motion.Y--;
            if (keyboardState.IsKeyDown(Keys.NumPad2))
                motion.Y++;
            if (keyboardState.IsKeyDown(Keys.NumPad7))
            {
                motion.Y--;
                motion.X--;
            }
            if (keyboardState.IsKeyDown(Keys.NumPad9))
            {
                motion.Y--;
                motion.X++;
            }
            if (keyboardState.IsKeyDown(Keys.NumPad1))
            {
                motion.Y++;
                motion.X--;
            }
            if (keyboardState.IsKeyDown(Keys.NumPad3))
            {
                motion.Y++;
                motion.X++;
            }

            if (motion != Vector2.Zero)
            {
                motion.NormalizeForTile();
                Position += motion * Speed;
            }

            Position.X = (int)MathHelper.Clamp(Position.X, 0 - playerPosition.X, (CameraMovementRangeX - ScreenWidth) + playerPosition.X);
            Position.Y = (int)MathHelper.Clamp(Position.Y, 0 - playerPosition.Y, (CameraMovementRangeY - ScreenHeight) + playerPosition.Y);
        }
    }
}